package nl.alwinzwets.arduino
{
	
	import flash.events.Event;
	
	import net.eriksjodin.arduino.Arduino;
	import net.eriksjodin.arduino.events.ArduinoEvent;
	import net.eriksjodin.arduino.events.ArduinoSysExEvent;
	
	public class ArduinoClass
	{
		private var a:Arduino;
		private var numEvents:Number=0;		
		
		public function ArduinoClass()
		{
			// connect to a serial proxy on port 5331
			a = new Arduino("127.0.0.1", 5331);
					 	
			// listen for connection 
			a.addEventListener(Event.CONNECT,onSocketConnect); 
			a.addEventListener(Event.CLOSE,onSocketClose);
						
			// listen for firmware (sent on startup)
			a.addEventListener(ArduinoEvent.FIRMWARE_VERSION, onReceiveFirmwareVersion);
			
			// listen for data
			a.addEventListener(ArduinoEvent.ANALOG_DATA, onReceiveAnalogData); 
			a.addEventListener(ArduinoEvent.DIGITAL_DATA, onReceiveDigitalData);
			
			//listen for sysex messages
			a.addEventListener(ArduinoSysExEvent.SYSEX_MESSAGE, onReceiveSysExMessage);	
			
		}
		
			// triggered when a serial socket connection has been established
			private function onSocketConnect(e:Object):void {
				trace("Socket connected!");
				// request the firmware version
				a.requestFirmwareVersion();
				trace("Requesting firmware version...");
				
			}
					
			// triggered when a serial socket connection has been closed
			private function onSocketClose(e:Object):void {
				trace("Socket closed!");
			}
					
			// trace out data when it arrives...	
			private function onReceiveAnalogData(e:ArduinoEvent):void {
				trace((numEvents++) +" Analog pin " + e.pin + " on port: " + e.port +" = " + e.value);
			}
					
			// trace out data when it arrives...
			private function onReceiveDigitalData(e:ArduinoEvent):void {
				trace((numEvents++) +" Digital pin " + e.pin + " on port: " + e.port +" = " + e.value);
			}
			
			// trace incoming sysex messages
			private function onReceiveSysExMessage(e:ArduinoSysExEvent):void {
				trace((numEvents++) +"Received SysExMessage. Command:"+e.data[0]);
			}
			
			
			// the firmware version is requested when the Arduino class has made a socket connection.
			// when we receive this event we know that the Arduino has been successfully connected.
			private function onReceiveFirmwareVersion(e:ArduinoEvent):void {
				trace("Firmware version: " + e.value);
				if(int(e.value)!=2) {
					trace("Unexpected Firmware version encountered! This Version of as3glue was written for Firmata2.");
				}
				// the port value of an event can be used to determine which board the event was dispatched from
				// this is one way of dealing with multiple boards, another is to add different listener methods
				trace("Port: " + e.port);
						
				// do some stuff on the Arduino...
				initArduino();
			}
					
			private function initArduino():void {
				
				trace("Initializing Arduino");
				
				// set a pin to output
				a.setPinMode(13, Arduino.INPUT);
						
				
				
				// enable reporting for digital pins
				a.enableDigitalPinReporting();
						
				// disable reporting for digital pins
				//a.disableDigitalPinReporting();
						
				// enable reporting for an analog pin
				//a.setAnalogPinReporting(4, Arduino.ON);
						
				// disable reporting for an analog pin
				//a.setAnalogPinReporting(3, Arduino.OFF);
						
				
			}
			
			public function setDigitalPin(pin:int, mode:String):void
			{
				if(mode == "LOW"){
					a.writeDigitalPin(pin, Arduino.LOW);	
				} else {
					a.writeDigitalPin(pin, Arduino.HIGH);
				}
			}

	}
}